package com.yanqu.road.server.manager.guess.playtypehandler.handler;

import com.yanqu.road.entity.guess.CommonGuessInitReq;
import com.yanqu.road.entity.guess.CommonGuessRewardItem;
import com.yanqu.road.entity.guess.data.CommonGuessItem;
import com.yanqu.road.entity.guess.data.CommonGuessMain;
import com.yanqu.road.entity.guess.emuns.CommonGuessPlayTypeEnum;
import com.yanqu.road.server.manager.CrossDataAnalyticsMgr;
import com.yanqu.road.server.manager.guess.playtypehandler.CommonGuessPlayTypeStrategy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 冠亚军竞猜策略
 */
public class CommonGuessTopThreeStrategy implements CommonGuessPlayTypeStrategy {

    private static Logger logger = LogManager.getLogger(CommonGuessTopThreeStrategy.class.getName());

    /**
     * 获取竞猜中的奖励档次信息
     * @param myObjIds
     * @param guessMain
     * @param guessItemMap
     * @return
     */
    @Override
    public CommonGuessRewardItem getRewardItem(String myObjIds, CommonGuessMain guessMain, Map<String, CommonGuessItem> guessItemMap) {
        List<CommonGuessRewardItem> rewardItemList = guessMain.getReward();
        List<String> myObjIdList = Arrays.asList(myObjIds.split(","));//我的竞猜
        List<String> resultObjIdsList = Arrays.asList(guessMain.getRankResult().split(","));//结果
        int rewardIndex = 4;
        //一模一样，一等奖
        if (myObjIds.equals(guessMain.getRankResult())) {
            rewardIndex = 0;
        } else {
            for (int i = 0; i < CommonGuessPlayTypeEnum.PLAY_TYPE_TOP_THREE.getGuessNum(); i++) {
                boolean flag = false;
                if (myObjIdList.get(i).equals(resultObjIdsList.get(i))) {
                    flag = true;
                }
                if (flag) {
                    //猜中冠军
                    if (i == 0) {
                        rewardIndex = 1;
                    }
                    //猜中亚军
                    if (i == 1) {
                        rewardIndex = 2;
                    }
                    //猜中季军
                    if (i == 2) {
                        rewardIndex = 3;
                    }
                    break;
                }
            }
        }
        if (rewardIndex >= rewardItemList.size()) {
            return null;
        }
        return rewardItemList.get(rewardIndex);
    }

    /**
     * 初始化检查
     * @param req
     * @return
     */
    @Override
    public boolean initCheck(CommonGuessInitReq req) {
        //判断选项池是否大于选择的次数
        if (CommonGuessPlayTypeEnum.PLAY_TYPE_TOP_THREE.getGuessNum() >= req.getObjIdList().size()) {
            logger.error("通用竞猜初始化-选项池子配置错误：{}", req);
            return false;
        }
        //判断奖励LIST
        if (req.getRewardList().isEmpty() || req.getRewardList().size() < CommonGuessPlayTypeEnum.PLAY_TYPE_TOP_THREE.getRewardItemNum()) {
            logger.error("通用竞猜初始化-奖励项目配置错误：{}", req);
            return false;
        }
        return true;
    }
}
